package com.biostime.qdingding.modules.qd.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.biostime.qdingding.common.api.FastResponse;
import com.biostime.qdingding.common.api.ListResponse;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.api.dto.CouponDto;
import com.biostime.qdingding.modules.qd.entity.Course;
import com.biostime.qdingding.modules.qd.service.CouponService;
import com.biostime.qdingding.modules.qd.service.CourseService;
import com.biostime.qdingding.modules.qd.service.PriceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * 优惠券模块
 * 
 * @author chenweicheng
 * @version 2015-02-17
 */
@RestController
@RequestMapping(value = "${adminPath}/qd/api/coupon")
public class CouponApi extends BaseController{
	
	@Autowired
	private CouponService couponService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private PriceService moneyService;	
	
	/**
	 * 获取我的优惠券
	 * @return
	 */
	@RequestMapping(value="myCoupons")
	public ListResponse<CouponDto> myCoupons(String userId,int pagesize, int pageindex){
		int errCode = 0;
		String message  = null;
		List<CouponDto> coupons  = new ArrayList<CouponDto>();		
		if(userId != null && !userId.equals("")){
			PageHelper.startPage(pageindex, pagesize);
			coupons = couponService.myCoupons(userId,null,null);
			message = "获取我的优惠券列表成功";
		}else{
			message = "获取我的优惠列表失败,参数不全";
			errCode = 1;
		}
		
		PageInfo<CouponDto> page = new PageInfo<CouponDto>(coupons);		
		return FastResponse.create().errCode(errCode).subCode(0).errMsg(message).pageIndex(page.getPageNum())
				.pageSize(page.getPageSize()).total((int) page.getTotal()).hasNext(page.isHasNextPage())
				.list(coupons);
	}
	
	/**
	 * 获取可用优惠券列表
	 * @return
	 * @throws Exception 
	 */	
	@RequestMapping(value="chooseCoupon")
	public ListResponse<CouponDto> chooseCoupon(HttpServletRequest request,int pagesize,int pageindex) throws Exception{ 
		int errCode = 0;
		String message  = null;
		List<CouponDto> coupons  = new ArrayList<CouponDto>();	
		String userId = request.getParameter("userId");
		String studentId = request.getParameter("studentId");
		String courseId = request.getParameter("courseId");
		String centerId = request.getParameter("centerId");
		double moneyAfter=0d;
		
		if(userId != null && !userId.equals("") && courseId != null && !courseId.equals("") && studentId != null && !studentId.equals("")){
			Course course = courseService.get(courseId);
			String type = null;
			if(course != null){
				type = String.valueOf(course.getType());
			}
			PageHelper.startPage(pageindex, pagesize);
			coupons = couponService.myCoupons(userId,"%"+type+"%",centerId);
				
			//获取优惠后价格
			HashMap<String,Object> priceMap = moneyService.getPriceMap(course, studentId);
		
			if(priceMap != null){
				moneyAfter = (double) priceMap.get("moneyAfter");
			}
					
			//如果优惠券为折扣券，则判断课程价格是否大于生效金额
			if(coupons != null && !coupons.isEmpty()){
				for(int i =0 ;i<coupons.size();i++){
					if(coupons.get(i).getType().equals("deduction") && coupons.get(i).getEffectMoney() > moneyAfter){
						coupons.remove(i);
					}
				}
			}	
			
			message = "获取可用优惠券列表成功";
			
		}else{
			message = "获取可用优惠列表失败,参数不全";
			errCode = 1;
		}
		
		PageInfo<CouponDto> page = new PageInfo<CouponDto>(coupons);		
		return FastResponse.create().errCode(errCode).subCode(0).errMsg(message).pageIndex(page.getPageNum())
				.pageSize(page.getPageSize()).total((int) page.getTotal()).hasNext(page.isHasNextPage())
				.list(coupons);
	}	
		
}
